<template>
  <view class="quest">
    <view class="wrap">
      <view class="t">
        行为类
      </view>
      <view class="problem">
        <view class="p">任务名称:{{ quest.title }}</view>
        <view class="">
          {{ quest.taskText }}
        </view>
        <image v-if="quest.titleImg" :src="quest.titleImg" class="img" @click="showImage(1,quest.titleImg)">
        </image>
      </view>

      <view v-if="idsData.locked" class="imgBox">
        <view v-if="imgList.length == 0" class="bigUpImg" @click="uploadImg">
          <image class="cameraIcon"
                 src="https://travel20231111.oss-cn-hangzhou.aliyuncs.com/image/taskCameraIcon.png">
          </image>
        </view>
        <view v-else class="sl-img">
          <image :src="imgList[0]" class="img" @click="showImage(2,0)"></image>
          <view v-if="isComplete == false" class="icon" @click="deleteImg(0)">
            <u-icon color="#fff" name="trash" size="20"></u-icon>
          </view>
        </view>
        <view class="minUpImg">
          <view v-for="(item,index) in imgList" :key="index">
            <view v-if="index + 1  < imgList.length"
                  :class="['minSl-img',(index - 3) % 4 == 0?'nothingMargin':'']">
              <image :src="imgList[index + 1]" class="img" @click="showImage(2,index + 1)">
              </image>
              <view v-if="isComplete == false" class="icon" @click="deleteImg(index + 1)">
                <u-icon color="#fff" name="trash" size="20"></u-icon>
              </view>
            </view>
            <view v-if="index + 1 ==  imgList.length && isComplete == false" class="minCameraIcon"
                  @click="uploadImg">
              <image class="cameraIcon"
                     src="https://travel20231111.oss-cn-hangzhou.aliyuncs.com/image/taskCameraIcon.png">
              </image>
            </view>
          </view>
        </view>

      </view>
    </view>

  </view>
</template>

<script>
import {
  uploadImage
} from '@/api/upload.js'

export default {
  data() {
    return {
      abc: [
        'A', 'B', 'C', 'D', 'E', 'F', 'G'
      ],
      i: null,
      abcList: [],
      iList: [],
      topicText: '单选题',
      choiceBoole: true,
      imgList: [],
      isComplete: false,
    }
  },
  props: {
    quest: {
      type: Object,
      default: {}
    },
    completeCount: {
      typeof: Number,
      default: 0
    },
    sum: {
      typeof: Number,
      default: 0
    },
    idsData: {
      type: Object,
      default: {},
    }
  },
  computed: {
    newCompleteCount() {
      return this.completeCount + 1 > this.sum ? this.sum : this.completeCount + 1
    }
  },
  onReady() {
    this.isComplete = this.quest.isComplete
    if (this.isComplete == true) {
      this.imgList = this.quest.userImg.split(',')
    }
  },
  methods: {
    deleteImg(data) {
      this.imgList.splice(data, 1)
      this.img = ""
      this.imgList.forEach((item, index) => {
        if ((index + 1) == this.imgList.length) {
          this.img += item
        } else {
          this.img += item + ','
        }
      })
      this.$emit('uploadImg', this.img)
    },
    /**查看图片 */
    showImage(index, data) {
      if (index == 1) {
        uni.previewImage({
          current: data,
          urls: [data]
        })
      } else {
        uni.previewImage({
          current: this.imgList[data],
          urls: [this.imgList[data]]
        })
      }

    },
    clear() {
      this.i = null
    },
    choice(abc) {
      if (this.choiceBoole) {
        this.abcList = []
        for (let i = 0; i < this.quest.trueSelect.length; i++) {
          this.abcList.push(this.quest.trueSelect[i])
        }
        console.log('看', this.abcList);
        if (this.abcList.length > 1) {
          console.log(this.iList.indexOf(abc));
          if (this.iList.indexOf(abc) == -1) {
            this.iList.push(abc)
          } else {
            this.iList.splice(this.iList.indexOf(abc), 1)
          }

        } else {
          this.iList = [abc]
        }

        console.log('结果', this.iList);
        // this.i = abc
        this.$emit('choice', this.iList)
      }

    },
    onSubmit() {
      uni.showModal({
        content: '提交成功',
        cancelText: "重新选择",
        confirmText: "继续提交",
        success: (res) => {
          if (res.confirm) {
            this.toUrl('/pages/challenge/quest2')
          }
        }
      })
    },
    toUrl(url) {
      if (!url) return
      uni.navigateTo({
        url
      })
    },
    uploadImg() {
      uploadImage().then(res => {
        this.imgList.push(res)
        this.img = ""
        this.imgList.forEach((item, index) => {
          if ((index + 1) == this.imgList.length) {
            this.img += item
          } else {
            this.img += item + ','
          }
        })
        this.$emit('uploadImg', this.img)
      })

    },
  }
}
</script>

<style lang="scss" scoped>
.quest {
  font-family: PingFang SC, PingFang SC-Heavy;

  .wrap {
    width: 100%;
    padding: 40rpx 30rpx;
    box-sizing: border-box;

    .t {
      width: 156rpx;
      height: 72rpx;
      line-height: 72rpx;
      text-align: center;
      background: #4186f5;
      border-radius: 6px 6px 0px 0px;
      color: #fff;
    }
  }

  .problem {

    background: #ffffff;
    border-radius: 0 12rpx 12rpx 12rpx;
    box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(0, 0, 0, 0.10);
    padding: 30rpx 40rpx 10rpx 40rpx;

    .p {
      font-size: 36rpx;
      font-weight: 800;
      color: #212121;
      margin-bottom: 20rpx;
    }

    .img {
      width: 100%;
      height: 274rpx;
      margin-top: 20rpx;
    }
  }

  .imgBox {
    margin-top: 40rpx;
    border-radius: 0 12rpx 12rpx 12rpx;
    box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(0, 0, 0, 0.10);
    padding: 20rpx 40rpx;

    .bigUpImg {
      width: 100%;
      height: 300rpx;
      border: 1px dashed #dddddd;
      display: flex;
      justify-content: center;
      align-items: center;

      .cameraIcon {
        width: 120rpx;
        height: 108rpx;
      }
    }


    .sl-img {
      width: 100%;
      height: 300rpx;
      margin-bottom: 15rpx;
      position: relative;

      .img {
        width: 100%;
        height: 100%;
        border-radius: 10rpx;
      }

      .icon {
        position: absolute;
        z-index: 1;
        bottom: 0rpx;
        right: 0rpx;
        width: 40rpx;
        height: 40rpx;
        background: rgba(0, 0, 0, 0.30);
        border-radius: 6px 0px 6px 0px;
      }
    }

    .minUpImg {
      display: flex;
      flex-wrap: wrap;

      .minSl-img {
        width: 136rpx;
        height: 136rpx;
        position: relative;
        margin: 0 20rpx 20rpx 0;

        .img {
          width: 100%;
          height: 100%;
          border-radius: 10rpx;
        }

        .icon {
          position: absolute;
          z-index: 1;
          bottom: 0rpx;
          right: 0rpx;
          width: 40rpx;
          height: 40rpx;
          background: rgba(0, 0, 0, 0.30);
          border-radius: 6px 0px 6px 0px;
        }


      }

      .nothingMargin {
        margin-right: 0rpx;
      }


    }

    .minCameraIcon {
      width: 136rpx;
      height: 136rpx;
      border: 1px dashed #dddddd;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 10rpx;

      .cameraIcon {
        width: 48rpx;
        height: 42rpx;
      }
    }
  }
}
</style>
